package org.hxzon.demo.jacob;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JacobDemo2 {

    public static void main(String[] args) {
        ActiveXComponent excelAtx = new ActiveXComponent("Excel.Application");
        Dispatch excelObj = excelAtx.getObject();
        try {
            System.out.println("version=" + excelAtx.getProperty("Version"));
            System.out.println("version=" + Dispatch.get(excelObj, "Version"));
            excelAtx.setProperty("Visible", new Variant(true));
            Dispatch workbooks = excelAtx.getProperty("Workbooks").toDispatch();
            Dispatch workbook = Dispatch.get(workbooks, "Add").toDispatch();
            Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
            Dispatch a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A1" }, new int[1]).toDispatch();
            Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A2" }, new int[1]).toDispatch();
            Dispatch.put(a1, "Value", "123.456");
            Dispatch.put(a2, "Formula", "=A1*2");
            System.out.println("a1 from excel:" + Dispatch.get(a1, "Value"));
            System.out.println("a2 from excel:" + Dispatch.get(a2, "Value"));
            Variant f = new Variant(false);
            Dispatch.call(workbook, "Close", f);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            excelAtx.invoke("Quit", new Variant[] {});
        }

    }

}
